package com.xjw.coreutil.http;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import lombok.Data;

/**
 * http结果返回封装包
 *
 * @author 徐军文
 * @since 2021/11/6 0:17
 */
@Data
public class Result {

    private Boolean sucFlag;

    private int code;

    private Object data;

    private String errorMsg;

    public static Result asyResult(String res) {
        Result result = JsonUtil.parseObject(res, Result.class);
        result.setData(result.getData());
        return result;
    }

    public static String getSucRes(Object obj) {
        Result result = new Result();
        result.setCode(2000);
        result.setSucFlag(true);
        result.setData(obj);
        return JsonUtil.objectToString(result);
    }

    public static String getErrorRes(String msg) {
        Result result = new Result();
        result.setCode(5000);
        result.setSucFlag(false);
        result.setErrorMsg(msg);
        return JSON.toJSONString(result, JSONWriter.Feature.WriteMapNullValue, JSONWriter.Feature.WriteNonStringKeyAsString);
    }

    public static String getUnKnowErrorRes(String msg) {
        Result result = new Result();
        result.setCode(6000);
        result.setSucFlag(false);
        result.setErrorMsg(msg);
        return JSON.toJSONString(result, JSONWriter.Feature.WriteMapNullValue, JSONWriter.Feature.WriteNonStringKeyAsString);
    }

}
